<%@ include file="/en/jsp/static.jsp" %>
<%@ taglib uri="/linuxbox/common.tld" prefix="common" %>
<%@ page import="linuxbox.bookmark.logic.*" %>

<jsp:include page="/en/jsp/header.jsp" flush="true"/>

<%
  BookmarkHome home = BookmarkHome.getInstance();

  String VIEW_TYPE = (String) session.getAttribute("bookmark.VIEW_TYPE");
  if (VIEW_TYPE==null)
  {
    VIEW_TYPE = home.VT_ALL;
    session.setAttribute("bookmark.VIEW_TYPE", VIEW_TYPE);
  }

  boolean viewAll = false;
  if ( home.VT_MINE.compareTo(VIEW_TYPE) != 0
    &&   home.VT_SYSTEM.compareTo(VIEW_TYPE) != 0)
    viewAll = true;

  String SORTBY = (String) session.getAttribute("bookmark.SORTBY");
  if (SORTBY==null)
  {
    SORTBY = "name";
    session.setAttribute("bookmark.SORTBY", SORTBY);
  }

  boolean sortAsc = true;
  String tmp = (String) session.getAttribute("bookmark.ASC");
  if (tmp!=null && tmp.length() > 0)
    sortAsc = new Boolean(tmp).booleanValue();
  else
    session.setAttribute("bookmark.ASC", "true");


  int curPage = 0;
  String PAGE = (String) request.getParameter("PAGE");
  if (PAGE != null)
    curPage = Integer.parseInt(PAGE);

  int entriesPerPage = sec.getConfigInt("bookmark", "General", "Entries Per Page");
  int totalPage = home.getTotalPages(curID, entriesPerPage, VIEW_TYPE);
  if (curPage >= totalPage)
    curPage = totalPage-1;


  Collection col = home.getBookmarks(curID, VIEW_TYPE, SORTBY, sortAsc, entriesPerPage*curPage, entriesPerPage);
  int count = 0;
  String bookmarkJSP = BoxStatic.jspDir+"bookmark/Bookmark.jsp";
  String bookmarkAction = BoxStatic.servletDir+"BookmarkAction";
%>

<script Language="JavaScript">
  function confirmDel(entry)
  {
    return confirm("Are you sure to delete " + entry);
  }
  function onChangeViewType()
  {
    document.theForm.submit();
  }
</script>

<FORM action="<%=BoxStatic.servletDir%>BookmarkAction" name="theForm" method="POST">

<table width="100%" height=26 align=middle border=0 cellpadding=0 cellspacing=0 bgcolor="#f4f4f4" style="font-size:16px;color:#d05000">
  <tr>
    <td>
      &nbsp;<img src="<%=BoxStatic.imageDir%>itemball.jpg" width=14 height=15 border=0 align="absmiddle">
      <b><%=boxName%> - Bookmarks</b>
    </td>
    <td align=right>
      <a href="<%=BoxStatic.servletDir%>BookmarkSettingsAction?SubmitEdit=edit">
        Settings
      </a>
    </td>
  </tr>
</table>
<hr width=\"100%\" color=#E06000 align=left>

<table width="100%" border=0 cellpadding=0 cellspacing=0>
<tr><td>
  <table width="100%">
    <tr>
      <td>
        <input type="submit" name="SubmitEdit" value="Add a bookmark">
      </td>
      <td>View
          <select name="VIEW_TYPE" size="1" onchange="onChangeViewType()">
            <option <% if (VIEW_TYPE.compareTo(home.VT_ALL) == 0) out.print(" selected ");%> value="<%=home.VT_ALL%>">All</option>
            <option <% if (VIEW_TYPE.compareTo(home.VT_SYSTEM) == 0) out.print(" selected ");%> value="<%=home.VT_SYSTEM%>">System bookmarks</option>
            <option <% if (VIEW_TYPE.compareTo(home.VT_MINE) == 0) out.print(" selected ");%> value="<%=home.VT_MINE%>">My bookmarks</option>
          </select>
      </td>
      <td align=right>
        <common:Page curPage="<%=curPage%>" totalPage="<%=totalPage%>" action="<%=bookmarkJSP%>" />
      </td>
    </tr>
  </table>
</td></tr>

<tr><td>
<%

  if ( !col.isEmpty())
  {
    String[] headers = {"Name", "URL", "Edit","Delete"};
    String[] sortby = {"name", "url", "",""};
    String[] allheaders = {"Name", "URL", "CreatedBy", "Edit","Delete"};
    String[] allsortby = {"name", "url", "userid", "", ""};

    if ( viewAll)
    {
      headers = allheaders;
      sortby = allsortby;
    }

    Iterator it = col.iterator();
%>
  <TABLE cellSpacing=1 cellPadding=0 width="100%" border=0 bgColor=#dddddd>
    <TR bgColor=#7EC1F3>
      <common:ListHeader headers="<%=headers%>" sortby="<%=sortby%>" curSortBy="<%=SORTBY%>" sortAsc="<%=sortAsc%>" action="<%=bookmarkAction%>" />
    </TR>

    <common:Iteration name="entry" type="linuxbox.bookmark.logic.BookmarkEntry" group="<%=it%>">
      <tr <% if ((count++ % 2) == 0) out.print("bgcolor=#f0f0f0"); %>>
        <TD><a href="<%=entry.getUrl()%>" target="_blank"><%=entry.getName()%></a></TD>
        <TD><%=entry.getUrl()%></TD>
<%
    if (viewAll)
    {
      out.print("<TD align=center>");
      if (entry.getUserid() == -1)
        out.print("System");
      else
        out.print("You");
      out.print("</TD>");
    }
    if (sec.isBookmarkAdmin() || entry.getUserid() == curID)
    {
%>
        <TD><a href="<%=BoxStatic.servletDir%>BookmarkAction?SubmitEdit=edit&ID=<%=entry.getID()%>">Edit</a></TD>
        <TD  align=center >
            <a href="<%=BoxStatic.servletDir%>BookmarkAction?SubmitDelete=del&ID=<%=entry.getID()%>"
              Onclick='return confirmDel("<%=entry.getName()%>")'
              ALT="Delete">Delete</a>
        </TD>
<%
    }
    else
      out.print("<TD></TD><TD></TD>");
%>
      </TR>
    </common:Iteration>
  </TABLE>
</td></tr>

<tr><td>
  <table width="100%">
    <tr>
      <td>
        <input type="submit" name="SubmitEdit" value="Add a bookmark">
      </td>
      <td>View
          <select name="VIEW_TYPE" size="1" onchange="onChangeViewType()">
            <option <% if (VIEW_TYPE.compareTo(home.VT_ALL) == 0) out.print(" selected ");%> value="<%=home.VT_ALL%>">All</option>
            <option <% if (VIEW_TYPE.compareTo(home.VT_SYSTEM) == 0) out.print(" selected ");%> value="<%=home.VT_SYSTEM%>">System bookmarks</option>
            <option <% if (VIEW_TYPE.compareTo(home.VT_MINE) == 0) out.print(" selected ");%> value="<%=home.VT_MINE%>">My bookmarks</option>
          </select>
      </td>
      <td align=right>
        <common:Page curPage="<%=curPage%>" totalPage="<%=totalPage%>" action="<%=bookmarkJSP%>" />
      </td>
    </tr>
  </table>
</td></tr>

<%
  }
  else
  {
%>
    <TABLE cellSpacing=1 cellPadding=1 width="100%" border=0 bgColor=#dddddd>
      <TR>
        <TD bgColor=#eeeeee>No bookmarks to be shown.</TD>
      </TR>

    </TABLE>
</td></tr>
<%
  }
%>
<tr><td align=center>
<br>
</td></tr>

<tr><td align=center>
  <table cellSpacing=1 cellPadding=1  border=0 bgColor=#eeeeee>
    <tr><td align=center>
    <b>Quick Add <% if (home.VT_SYSTEM.compareTo(VIEW_TYPE) == 0) out.print("System"); %>
       Bookmark</b>
    </td></tr>
    <tr><td align=center>
      <table>
        <tr>
          <td>
            <b>Name:</b> <input align=right type=text name=NAME size=20 value="">
          </td>
          <td>
            <b>URL:</b>  <input align=right type=text name=URL size=40 value="http://">
          </td>
          <td>
            <input type="submit" name="<%
              if (home.VT_SYSTEM.compareTo(VIEW_TYPE) == 0)
                out.print("SubmitSysSave");
              else
                out.print("SubmitSave");
            %>" value="Add">
          </td>
        </tr>
      </table>
    </td></tr>
  </table>
</td></tr>

</table>
<hr width=\"100%\" color=#E06000 align=left>

</FORM>

<jsp:include page="/en/jsp/footer.jsp" flush="true"/>
